package controllers

import (
	"github.com/gin-gonic/gin"
	"my_gin/controllers/tests"
	"my_gin/utils"
	"net/http"
	"sync"
)

var once sync.Once

// LoadRoutes 注册路由
func LoadRoutes(r *gin.Engine) {
	once.Do(func() {
		r.Static("/assets", "./assets")                      // 静态资源目录
		r.StaticFile("/favicon.ico", "./assets/favicon.ico") // favicon.ico
		r.GET("/", defaultIndex)                             // 默认首页
		r.GET("/test/rand_string", tests.RandString)         // 输出随机字符串
		r.GET("/test/captcha", tests.Captcha)                // 输出图片验证码
		r.POST("/test/login", tests.Login)                   // 用户登录提交，页面 /assets/login.html
	})
}

// 默认首页
func defaultIndex(c *gin.Context) {
	welcome, ok := utils.GetEnvByKey("WELCOME")
	if !ok {
		welcome = "welcome"
	}
	c.JSON(http.StatusOK, utils.GetApiResponseOK(gin.H{
		"msg": welcome,
	}))
}
